package ontology;



import java.util.HashMap;
import ontology.OntologyReader;
import org.jdom.Element;
import utils.InteractionRelationship;

public class InteractionType implements Comparable{
	
	private final InteractionVerb verb;
	private final HivProt hivProt;
        private final HashMap<String, InteractionRelationship> relationships = new HashMap();
	
	public InteractionType(InteractionVerb verb, HivProt hivProt){
		this.verb = verb;
		this.hivProt = hivProt;
	}

	public InteractionVerb getVerb() {
		return verb;
	}

	public HivProt getHivProt() {
		return hivProt;
	}
	
	@Override
	public String toString(){
		return hivProt.name()+"__"+verb.name();
	}

    public static InteractionType fromString(String intTypeString, InteractionTypeFactory f){
        String split[] = intTypeString.split("__");
        return f.getType(InteractionVerb.getInteractionVerbByName(split[1]), HivProt.getProtByName(split[0]));
    }
	
	@Override
	public boolean equals(Object obj){
		if(!obj.getClass().equals(InteractionType.class)){
			return false;
		}
		InteractionType other = (InteractionType) obj;
		if(this.getHivProt().equals(other.getHivProt()) &&
				this.getVerb().equals(other.getVerb())){
			return true;
		}
		return false;
	}

	public int compareTo(Object otherInteractionType) {
		InteractionType other = (InteractionType) otherInteractionType;
		if(other.getHivProt().equals(this.getHivProt())){
			if(other.getVerb().equals(this.getVerb())){
				return 0;
			} else {
				return this.getVerb().compareTo(other.getVerb());
			}
		} else {
			return this.getHivProt().compareTo(other.getHivProt());
		}
	}

    public static InteractionType newFromString(String intTypeStr){
        
        String split[] = intTypeStr.split("__");
        HivProt hivProt = HivProt.getProtByName(split[0]);
        InteractionVerb verb = InteractionVerb.getInteractionVerbByName(split[1]);
        return new InteractionType(verb, hivProt);
    }

    public InteractionRelationship getInteractionRelationship(InteractionType otherType, OntologyReader reader){
        if(!this.getHivProt().equals(otherType.getHivProt())){
            return InteractionRelationship.SOLITARY;
        }
        Element thisElem = reader.getElement(this.getVerb().getDescription());
        Element otherElem = reader.getElement(otherType.getVerb().getDescription());
        int ident = ontology.Utils.couldBeSameAction(thisElem, otherElem);
        

        if(ident==0){
            return InteractionRelationship.SOLITARY;
        } else if(ident==1){
            return InteractionRelationship.PARENT;
        } else if(ident==2){
            return InteractionRelationship.CHILD;
        } else {
            return InteractionRelationship.SIBLING;
        }
    }

    public void setRelationship(String key, InteractionRelationship value){
        relationships.put(key, value);
    }

    public InteractionRelationship getRelationship(String key){
        return relationships.get(key);
    }

    public HashMap getDataHash(){
        return relationships;
    }

    public void emptyDataHash(){
        this.relationships.clear();
    }
	
	
	
	

}
